<?php
namespace app\admin\service;

use app\admin\model\ArcModel;
use app\admin\model\Category;
use app\admin\service\interface\CategoryService;
class CategoryServiceImpl implements CategoryService
{
  
    public function add(string $name,int $model_id,int $pid=0){
        if(!ArcModel::find($model_id)){
            throw new \Exception("模型不存在！");
        }
        if($pid>0 && !Category::find($pid)){
            throw new \Exception("父栏目不存在！");
        }
        $category = new Category();
        $category->name=$name;
        $category->pid=$pid;
        $category->model_id=$model_id;
        return $category->save();
    }
    public function edit(int $id,string $name,int $model_id,int $pid=0){
        $category =Category::find($id);
        if(!$category){
            throw new \Exception("栏目不存在！");
        }
        if(!ArcModel::find($model_id)){
            throw new \Exception("模型不存在！");
        }
        if($pid>0 && !Category::find($pid)){
            throw new \Exception("父栏目不存在！");
        }
        
        $category->name=$name;
        $category->pid=$pid;
        $category->model_id=$model_id;
        return $category->save();
    }
    public function delete(int $id){
        
    }
    public function list(){
        
    }
}
